﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YJKArch.NumberRoom
{
	internal class RoomNumberDataOperator
	{
		public static RoomTagType GetRoomTagType(ExternalCommandData revit, bool bEngName, bool bUnit = true)
		{
			Autodesk.Revit.DB.Document document = revit.Application.ActiveUIDocument.Document;
			string tagTypeNameBySetting = RoomNumberDataOperator.GetTagTypeNameBySetting(bEngName, bUnit);
			RoomTagType roomTagType = null;
			foreach (Element element in ((IEnumerable<Element>)YJKElementFilter.GetElementByCategory(document, typeof(FamilySymbol), BuiltInCategory.OST_RoomTags)))
			{
				RoomTagType roomTagType2 = element as RoomTagType;
				if (roomTagType2 != null && roomTagType2.Name == tagTypeNameBySetting)
				{
					roomTagType = roomTagType2;
					break;
				}
			}
			if (roomTagType == null)
			{
				string text = Product.InstallLocation + "\\FamilyLib\\Family";
				string versionNumber = revit.Application.Application.VersionNumber;
				string tagFamilyNameByType = RoomNumberDataOperator.GetTagFamilyNameByType(tagTypeNameBySetting);
				text = string.Concat(new string[]
				{
					text,
					"\\",
					versionNumber,
					"\\RoomTag\\",
					tagFamilyNameByType,
					".rfa"
				});
				if (!File.Exists(text))
				{
					YJKMessageBox.Show(text + "文件不存在！");
					return null;
				}
				Family family = null;
				MyFamilyLoadOptions myFamilyLoadOptions = new MyFamilyLoadOptions();
				document.LoadFamily(text, myFamilyLoadOptions, out family);
				foreach (Element element2 in ((IEnumerable<Element>)YJKElementFilter.GetElementByCategory(document, typeof(FamilySymbol), BuiltInCategory.OST_RoomTags)))
				{
					RoomTagType roomTagType3 = element2 as RoomTagType;
					if (roomTagType3 != null && roomTagType3.Name == tagTypeNameBySetting)
					{
						roomTagType = roomTagType3;
						break;
					}
				}
			}
			return roomTagType;
		}

		public static string GetTagTypeNameBySetting(bool bEngName, bool bUnit)
		{
			string result;
			if (RoomNumberDataOperator.GetDimNameOrNot())
			{
				if (bEngName)
				{
					if (RoomNumberDataOperator.GetDimNumOrNot())
					{
						if (RoomNumberDataOperator.GetDimAreaOrNot())
						{
							if (bUnit & RoomNumberDataOperator.GetDimUnitOrNot())
							{
								result = "名称_换行_编号_面积_单元";
							}
							else
							{
								result = "名称_换行_编号_面积_无单元";
							}
						}
						else if (bUnit & RoomNumberDataOperator.GetDimUnitOrNot())
						{
							result = "名称_换行_编号_无面积_单元";
						}
						else
						{
							result = "名称_换行_编号_无面积_无单元";
						}
					}
					else if (RoomNumberDataOperator.GetDimAreaOrNot())
					{
						if (bUnit & RoomNumberDataOperator.GetDimUnitOrNot())
						{
							result = "名称_换行_无编号_面积_单元";
						}
						else
						{
							result = "名称_换行_无编号_面积_无单元";
						}
					}
					else if (bUnit & RoomNumberDataOperator.GetDimUnitOrNot())
					{
						result = "名称_换行_无编号_无面积_单元";
					}
					else
					{
						result = "名称_换行_无编号_无面积_无单元";
					}
				}
				else if (RoomNumberDataOperator.GetDimNumOrNot())
				{
					if (RoomNumberDataOperator.GetDimAreaOrNot())
					{
						if (bUnit & RoomNumberDataOperator.GetDimUnitOrNot())
						{
							result = "名称_编号_面积_单元";
						}
						else
						{
							result = "名称_编号_面积_无单元";
						}
					}
					else if (bUnit & RoomNumberDataOperator.GetDimUnitOrNot())
					{
						result = "名称_编号_无面积_单元";
					}
					else
					{
						result = "名称_编号_无面积_无单元";
					}
				}
				else if (RoomNumberDataOperator.GetDimAreaOrNot())
				{
					if (bUnit & RoomNumberDataOperator.GetDimUnitOrNot())
					{
						result = "名称_无编号_面积_单元";
					}
					else
					{
						result = "名称_无编号_面积_无单元";
					}
				}
				else if (bUnit & RoomNumberDataOperator.GetDimUnitOrNot())
				{
					result = "名称_无编号_无面积_单元";
				}
				else
				{
					result = "名称_无编号_无面积_无单元";
				}
			}
			else if (RoomNumberDataOperator.GetDimNumOrNot())
			{
				if (RoomNumberDataOperator.GetDimAreaOrNot())
				{
					if (bUnit & RoomNumberDataOperator.GetDimUnitOrNot())
					{
						result = "无名称_编号_面积_单元";
					}
					else
					{
						result = "无名称_编号_面积_无单元";
					}
				}
				else if (bUnit & RoomNumberDataOperator.GetDimUnitOrNot())
				{
					result = "无名称_编号_无面积_单元";
				}
				else
				{
					result = "无名称_编号_无面积_无单元";
				}
			}
			else if (RoomNumberDataOperator.GetDimAreaOrNot())
			{
				if (bUnit & RoomNumberDataOperator.GetDimUnitOrNot())
				{
					result = "无名称_无编号_面积_单元";
				}
				else
				{
					result = "无名称_无编号_面积_无单元";
				}
			}
			else if (bUnit & RoomNumberDataOperator.GetDimUnitOrNot())
			{
				result = "无名称_无编号_无面积_单元";
			}
			else
			{
				result = "无名称_无编号_无面积_无单元";
			}
			return result;
		}

		public static string GetTagFamilyNameByType(string type)
		{
			return "盈建科房间标记";
		}

		public static XmlNode GetSettingDataNode()
		{
			string text = Product.UserDataLocation + "\\RoomNumberSettings.xml";
			if (!File.Exists(text))
			{
				return null;
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(text);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("RoomNumberSettingsData");
			if (xmlNode == null)
			{
				return null;
			}
			XmlNode xmlNode2 = xmlNode.SelectSingleNode("Data");
			if (xmlNode2 == null)
			{
				return null;
			}
			return xmlNode2;
		}

		public static bool GetDimNameOrNot()
		{
			bool result = false;
			XmlNode settingDataNode = RoomNumberDataOperator.GetSettingDataNode();
			if (settingDataNode != null)
			{
				result = Convert.ToBoolean(settingDataNode.SelectSingleNode("房间名称").InnerText);
			}
			return result;
		}

		public static bool GetDimUnitOrNot()
		{
			bool result = false;
			XmlNode settingDataNode = RoomNumberDataOperator.GetSettingDataNode();
			if (settingDataNode != null)
			{
				result = Convert.ToBoolean(settingDataNode.SelectSingleNode("单元").InnerText);
			}
			return result;
		}

		public static bool GetDimNumOrNot()
		{
			bool result = false;
			XmlNode settingDataNode = RoomNumberDataOperator.GetSettingDataNode();
			if (settingDataNode != null)
			{
				result = Convert.ToBoolean(settingDataNode.SelectSingleNode("房间编号").InnerText);
			}
			return result;
		}

		public static bool GetDimAreaOrNot()
		{
			bool result = false;
			XmlNode settingDataNode = RoomNumberDataOperator.GetSettingDataNode();
			if (settingDataNode != null)
			{
				result = Convert.ToBoolean(settingDataNode.SelectSingleNode("房间面积").InnerText);
			}
			return result;
		}

		public static bool GetDimAreaByTextOrNot()
		{
			bool result = true;
			XmlNode settingDataNode = RoomNumberDataOperator.GetSettingDataNode();
			if (settingDataNode != null)
			{
				result = Convert.ToBoolean(settingDataNode.SelectSingleNode("方式1").InnerText);
			}
			return result;
		}

		public static string GetPrefixString()
		{
			string result = "";
			XmlNode settingDataNode = RoomNumberDataOperator.GetSettingDataNode();
			if (settingDataNode != null)
			{
				result = settingDataNode.SelectSingleNode("前缀").InnerText;
			}
			return result;
		}

		public static string GetSuffixString()
		{
			string result = "";
			XmlNode settingDataNode = RoomNumberDataOperator.GetSettingDataNode();
			if (settingDataNode != null)
			{
				result = settingDataNode.SelectSingleNode("后缀").InnerText;
			}
			return result;
		}
	}
}
